/**
 * Created by zhangxiaoyu on 16/12/12.
 */
angular.module('hyt.abnormalService', ['ngResource'])

  .factory('AbnormalList', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getAbnormal,
      abnormalData={},pageSize=10;

    var resource = $resource(ApiUrl+':abnormalData',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getAbnormalData : function(userMsg){
        var hasNextPage = true;
        resource.query({
          abnormalData:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            userMsg:userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            abnormalData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AbnormalList.Updated');
        })
      },
      getResult:function(){
        if(abnormalData[pageSize]===undefined){
          return false;
        }
        return abnormalData[pageSize].data;
      },
      getMoreAbnormalData : function(userMsg){
        if(abnormalData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = abnormalData[pageSize].hasNextPage;
        var nextPage = abnormalData[pageSize].nextPage;
        var moreAbnormalData = abnormalData[pageSize].data;
        resource.query({
          abnormalData:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            userMsg:userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreAbnormalData = moreAbnormalData.concat(r.rows);

          abnormalData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreAbnormalData
          };
          $rootScope.$broadcast('AbnormalList.Updated');
        })
      },
      hasNextPage: function() {
        if (abnormalData[pageSize] === undefined) {
          return false;
        }
        return abnormalData[pageSize].hasNextPage;
      }
    }
  })
  .factory('SearchWb', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getWaybillByNo,
      result;

    var resource = $resource(ApiUrl+':searchWb',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWbList : function(searchWb){
        return resource.query({
          searchWb:searchWb
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SearchWb.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('SaveAbnormal', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.saveAbnormalMsg,
      result;

    var resource = $resource(ApiUrl+':reportAbnl',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSaveAbnl : function(reportAbnl){
        return resource.query({
          reportAbnl:reportAbnl
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SaveAbnl.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('UploadAbnormaImg', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.uploadAbnormaPicture,
      result;
    var resource = $resource(ApiUrl);
    return{
      getUpImg : function(upImg){
        return resource.save({
          wayBillId:upImg.wayBillId,
          fileName:upImg.fileName,
          fileContext:upImg.fileContext
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('UploadAbnormaImg.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
